<?php

namespace App\Http\Controllers;

use App\Http\Resources\CollectCollection;
use App\Http\Resources\Data;
use App\Http\Resources\DataCollection;
use App\Http\Resources\EarningCollection;
use App\Http\Resources\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;


class ProfileController extends Controller
{
    //修改用户信息
    public function updateinfo(Request $request)
    {
        if (!empty(request('name'))) {
            $data = $request->validate([
                'name' => 'required|string|max:255|unique:users',
            ]);
        }
        if (!empty(request('sign'))) {
            $data = $request->validate([
                'sign' => 'max:100',
            ]);
        }
        !empty(request('avatar')) ? $data['avatar'] = request('avatar') : '';
        request()->user()->update($data);
        return response(['status' => 1, 'message' => 'success']);
    }

    //绑定三方登录账号
    public function threebind($hashtoken)
    {
        if(isset($hashtoken)){
            $data = json_decode(Redis::get($hashtoken),true);
//            dd($data);
        }else{
            return response(['status' => 1, 'message' => '非法请求']);
        }
        $userinfo = request()->user();
        $user = [
            $data['type'].'name' => $data['nickname'],
            $data['type'].'openid' => $data['openid'],
        ];
        if($userinfo->avatar == ''){
            $user['avatar'] = $data['avatar'];
        }
        if($userinfo->update($user)){
            return response(['status' => 1, 'message' => '绑定成功']);
        }else{
            return response(['status' => 0, 'message' => '绑定失败']);
        }
    }
    //解除三方登录绑定
    public function threeunbind($type){
        $userinfo = request()->user();
        if($userinfo->update([$type.'name'=>'',$type.'openid'=>''])){
            return response(['status' => 1, 'message' => '解绑成功']);
        }else{
            return response(['status' => 0, 'message' => '解绑失败']);
        }
    }
    //用户信息
    public function userinfo($method)
    {
        $userinfo = request()->user();
        if ($userinfo->is_vip) {
            if ($userinfo->vipduetime < Carbon::now()) {
                $userinfo->is_vip = 0;
                $userinfo->save();
            }
        }
        return new User($userinfo);
    }

    //用户的收藏
    public function usercollects($type, $method)
    {
        $userinfo = request()->user();
        $page = 12;
        $where['type'] = $type;
        if ($type < 6) {
            $type == 3 ? $page = 8 : '';
            return new CollectCollection($userinfo->collects()->where($where)->orderBy('created_at', 'desc')->paginate($page));
        } else {
            return new CollectCollection($userinfo->collects()->where($where)->orderBy('created_at', 'desc')->paginate($page));
        }
    }

    //用户的上传记录
    public function useruploads($type)
    {
        $page = 12;
        $type == 3 ? $page = 8 : '';
        $userinfo = request()->user();
        $where['type'] = $type;
        $where['is_del'] = 0;
        return new DataCollection($userinfo->datas()->where($where)->orderBy('created_at', 'desc')->paginate($page));
    }

    //用户的下载
    public function userdowns($type)
    {
        $page = 12;
        $type == 3 ? $page = 8 : '';
        $userinfo = request()->user();
        $where['type'] = $type;
        return new EarningCollection($userinfo->downs()->where($where)->orderBy('created_at', 'desc')->paginate($page));
    }

    public function usermodeldel($id)
    {
        $data = \App\Data::find($id);
        if ($data && $data->user_id == request()->user()->id) {
            if ($data->status == 2) {
                if ($data->update(['is_del' => 1])) {
                    return response(['status' => 1, 'message' => '删除成功']);
                }
            }
        }
        return response(['status' => 0, 'message' => '删除失败']);
    }


}